// pages/home/health/health.js
var app = getApp;
const router = require('../../../config').router

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    navbar: [
      {
        cate_name: '健康饮食'
      }, {
        cate_name: '养生保健'
      }, {
        cate_name: '其他生活小常识'
      }
    ],
    banners: [
      // {
      //   adId: '50',
      //   associateId: '1074',
      //   adName: '金陵国花园打造华东地区最大的牡丹观赏园',
      //   adCode: '111',
      //   adImage: "http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180417/20180417135627436.jpg",
      //   adDepict: '',
      //   adUrl: '',
      //   video: 'http://www.jsxcpd.com/ftpdata/userfile/survery/video/20180417/20180417133848582.mp4'
      // }
    ],
    list: [
      // {
      //   id:'784',
      //   title: '冬季到江宁来看鱼',
      //   image: 'http://www.jsxcpd.com/ftpdata/userfile/survey/imges/20180125/20180125143731517.png',
      //   description: '在南京江宁的青龙大道有一个名叫鑫淼龙锦园的地方，很适合在节假日的时候去游玩。',
      //   createDateStr: '2018-01-19 14:46:50',
      //   video:'http://www.jsxcpd.com/ftpdata/userfile/survery/video/20180125/20180125143743802.mp4'
      // }
    ],
    windowHeight: '',
    windowWidth: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //健康养生轮播图
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'ad.query.healthyNews',
        v: '1.0',
        format: 'json'
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data.ad)
          that.setData({
            banners: res.data.ad
          })
        }
      }
    });
    //健康饮食
    wx.request({
      url: router,
      data: {
        appKey: 'w4q897jgvxkb',
        method: 'article.list.healthyDiet',
        v: '1.0',
        format: 'json'
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.error != 'error') {
          // console.log(res.data.article)
          that.setData({
            list: res.data.article
          })
        }
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  imageError(e) {
    var that = this
    var height = e.detail.height;
    var width = e.detail.width;
    var ratio = width / height
    var height1 = parseInt(that.data.windowWidth / ratio);
    console.log(height1)
    that.setData({
      height: height1 + 'px'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * tab切换
   */
  navbarTap: function (e) {
    var that = this;
    var tab = e.currentTarget.dataset.idx;
    this.setData({
      currentTab: tab
    })
    if (tab == 0){
      //健康饮食
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.healthyDiet',
          v: '1.0',
          format: 'json'
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data.article)
            that.setData({
              list: res.data.article
            })
          }
        }
      });
    } else if (tab == 1){
      //养生保健
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.healthCare',
          v: '1.0',
          format: 'json'
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data.article)
            that.setData({
              list: res.data.article
            })
          }
        }
      });
    } else if (tab == 2) {
      //生活小常识
      wx.request({
        url: router,
        data: {
          appKey: 'w4q897jgvxkb',
          method: 'article.list.livingKnowledge',
          v: '1.0',
          format: 'json'
        },
        success: function (res) {
          if (res.statusCode == 200 && res.data.error != 'error') {
            // console.log(res.data.article)
            that.setData({
              list: res.data.article
            })
          }
        }
      });
    }
  },
  
  /**
   * 详情
   */
  articleClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/index?id=' + id
    })
  }
})